package com.example.lin.wuhuan;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_show;
    private int widthPixels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_show = findViewById(R.id.iv_show);

        widthPixels = getResources().getDisplayMetrics().widthPixels;
        Bitmap bitmap = Bitmap.createBitmap(widthPixels, widthPixels, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        List<Huan> wuHuan = getWuHuan();

        for (Huan h : wuHuan) {
            paint.setColor(h.color);
            paint.setStrokeWidth(h.strokeWidth);
            canvas.drawCircle(h.cx, h.cy, h.r, paint);
        }

        iv_show.setImageBitmap(bitmap);
    }

    private List<Huan> getWuHuan() {
        ArrayList<Huan> list = new ArrayList<>();
        int centerX = widthPixels / 2;
        int centerY = widthPixels / 2;
        float d = widthPixels / 14.0f;
        float r = d * 5/ 3;
        int strokeWidth = (int) (d / 3.2f);

        Huan black = new Huan(r, centerX, centerY - r/2, Color.BLACK, strokeWidth);
        list.add(black);

        Huan blue = new Huan(r, black.cx - 2 * r - d, black.cy, Color.BLUE, strokeWidth);
        list.add(blue);

        Huan red = new Huan(r, black.cx + 2 * r + d, black.cy, Color.RED, strokeWidth);
        list.add(red);

        Huan yellow = new Huan(r, black.cx - r - d / 2, black.cy + r, Color.YELLOW, strokeWidth);
        list.add(yellow);
        Huan green = new Huan(r, black.cx + r + d / 2, black.cy + r, Color.GREEN, strokeWidth);
        list.add(green);
        return list;
    }


    class Huan {
        float r;
        float cx;
        float cy;
        int color;
        int strokeWidth;

        public Huan(float r, float cx, float cy, int color, int strokeWidth) {
            this.r = r;
            this.cx = cx;
            this.cy = cy;
            this.color = color;
            this.strokeWidth = strokeWidth;
        }
    }


}
